<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生注册</title>
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.1.1/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <style>
        body {
            background-color: #f5f5f5;
            padding-top: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .required-field:after {
            content: " *";
            color: red;
        }
        .btn-submit {
            margin-top: 20px;
        }
        .footer {
            text-align: center;
            margin-top: 20px;
            color: #777;
            font-size: 12px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2 class="form-title">学生信息注册</h2>
    <form class="form-horizontal" id="studentForm">
        <div class="form-group">
            <label for="studentId" class="col-sm-3 control-label required-field">学号</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="studentId" placeholder="请输入学号">
            </div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-3 control-label required-field">姓名</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="name" placeholder="请输入姓名">
            </div>
        </div>
        <div class="form-group">
            <label for="gender" class="col-sm-3 control-label required-field">性别</label>
            <div class="col-sm-9">
                <select class="form-control" id="gender">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="birthDate" class="col-sm-3 control-label">出生日期</label>
            <div class="col-sm-9">
                <input type="date" class="form-control" id="birthDate">
            </div>
        </div>
        <div class="form-group">
            <label for="idNumber" class="col-sm-3 control-label">身份证号</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="idNumber" placeholder="请输入身份证号">
            </div>
        </div>
        <div class="form-group">
            <label for="college" class="col-sm-3 control-label">学院</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="college" placeholder="请输入学院名称">
            </div>
        </div>
        <div class="form-group">
            <label for="major" class="col-sm-3 control-label">专业</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="major" placeholder="请输入专业名称">
            </div>
        </div>
        <div class="form-group">
            <label for="className" class="col-sm-3 control-label">班级</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="className" placeholder="请输入班级名称">
            </div>
        </div>
        <div class="form-group">
            <label for="status" class="col-sm-3 control-label">状态</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="status" placeholder="在校/离校等">
            </div>
        </div>
        <div class="form-group">
            <label for="dormId" class="col-sm-3 control-label">宿舍号</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="dormId" placeholder="请输入宿舍号">
            </div>
        </div>
        <div class="form-group">
            <label for="bedNumber" class="col-sm-3 control-label">床位号</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="bedNumber" placeholder="请输入床位号">
            </div>
        </div>
        <div class="form-group">
            <label for="phone" class="col-sm-3 control-label">手机号</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="phone" placeholder="请输入手机号">
            </div>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-3 control-label">邮箱</label>
            <div class="col-sm-9">
                <input type="email" class="form-control" id="email" placeholder="请输入邮箱">
            </div>
        </div>
        <div class="form-group">
            <label for="emergencyContact" class="col-sm-3 control-label">紧急联系人</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="emergencyContact" placeholder="请输入紧急联系人信息">
            </div>
        </div>
        <div class="form-group">
            <label for="username" class="col-sm-3 control-label required-field">用户名</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="username" placeholder="请输入用户名">
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-3 control-label required-field">密码</label>
            <div class="col-sm-9">
                <input type="password" class="form-control" id="password" placeholder="请输入密码">
            </div>
        </div>
        <div class="form-group">
            <label for="rePassword" class="col-sm-3 control-label required-field">确认密码</label>
            <div class="col-sm-9">
                <input type="password" class="form-control" id="rePassword" placeholder="请确认密码">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-9">
                <button type="button" class="btn btn-primary btn-block btn-submit" onclick="register()">注册</button>
                <button type="button" class="btn btn-default btn-block" onclick="window.location.href = 'login.html'">返回登录</button>
            </div>
        </div>
    </form>
    <div class="footer">
        <p>注意：带 * 的字段为必填项</p>
    </div>
</div>

<script>
    // 注册函数
    function register() {
        // 获取所有学生信息输入
        let studentId = document.getElementById("studentId").value;
        let name = document.getElementById("name").value;
        let gender = document.getElementById("gender").value;
        let birthDate = document.getElementById("birthDate").value;
        let idNumber = document.getElementById("idNumber").value;
        let college = document.getElementById("college").value;
        let major = document.getElementById("major").value;
        let className = document.getElementById("className").value;
        let status = document.getElementById("status").value;
        let dormId = document.getElementById("dormId").value;
        let bedNumber = document.getElementById("bedNumber").value;
        let phone = document.getElementById("phone").value;
        let email = document.getElementById("email").value;
        let emergencyContact = document.getElementById("emergencyContact").value;
        let username = document.getElementById("username").value;
        let password = document.getElementById("password").value;
        let rePassword = document.getElementById("rePassword").value;

        // 验证必填项
        if (studentId == '' || name == '' || gender == '' || username == '' || password == '') {
            alert("学号、姓名、性别、用户名和密码为必填项，不能为空");
            return;
        }

        if (password !== rePassword) {
            alert("两次密码输入不一致");
            return;
        }

        // 发送注册请求
        $.ajax({
            url: 'sreg',
            type: 'POST',
            data: {
                studentId: studentId,
                name: name,
                gender: gender,
                birthDate: birthDate,
                idNumber: idNumber,
                college: college,
                major: major,
                className: className,
                status: status,
                dormId: dormId,
                bedNumber: bedNumber,
                phone: phone,
                email: email,
                emergencyContact: emergencyContact,
                username: username,
                password: password
            },
            dataType: 'text',
            success: function(res) {
                try {
                    let result = JSON.parse(res);
                    if (result.code == 200) {
                        // 注册成功
                        alert("注册成功，请登录");

                        // 清空注册表单
                        document.getElementById("studentForm").reset();

                        // 跳转到登录页
                        window.location.href = 'login.html';
                    } else {
                        // 注册失败
                        alert("注册失败：" + result.msg);
                    }
                } catch(e) {
                    alert("注册失败：服务器响应格式错误");
                    console.error(e);
                    console.error("服务器响应:", res);
                }
            },
            error: function(xhr, status, error) {
                alert("注册请求失败，请稍后重试");
                console.error("注册请求失败:", status, error);
                console.error("响应信息:", xhr.responseText);
            }
        });
    }
</script>
</body>
</html>